const express = require("express")
const router = require('./router')
const cors = require('cors')
const app = express()

// 配置jsonp接口 在cors中间件之前配置 否则就变成了cors接口了
router.get('/jsonp', (req, res) => {
  // 获取回调函数
  const funcBack = req.query.callback
  const data = {
    name: 'zs',
    age: 18
  }
  const scriptStr = `${funcBack} (${JSON.stringify(data)})`
  res.send(scriptStr)
})

// 解决跨域
app.use(cors())
app.use(express.urlencoded({extended: false}))
app.use('/api', router)

app.listen(8088, () => {
  console.log('running')
})
